package com.xsm.activitygroup;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.message.BasicNameValuePair;

import com.xsm.internet.PostMessage;
import com.xsm.view.ClearEditText;
import com.xsm.view.LoadDialog;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

public class Nologin extends Activity{
	com.xsm.view.ClearEditText email;
	EmailHandler handler;
	String eString;
	int isrun=0;
	Context context;
	String result;
	LoadDialog dialog;
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
	super.onCreate(savedInstanceState);
	setContentView(R.layout.nologin);
	email=(ClearEditText)findViewById(R.id.fmail);
	handler=new EmailHandler();
	context=this;
}
public void Back(View view){
	finish();
}

public void Find(View view){
	dialog=new LoadDialog(context,"正在操作...");
	dialog.showDialog();
   eString=email.getText().toString().trim();
	if(isEmail(eString)){
		isrun=0;
	new Thread(new getResult()).start();	
	}else {
		Toast.makeText(this, "请输入正确的邮箱", Toast.LENGTH_LONG).show();
	}
}


public boolean isEmail(String email){
    if (null==email || "".equals(email)) return false;  
//  Pattern p = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}"); //简单匹配
    Pattern p =  Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");//复杂匹配
    Matcher m = p.matcher(email);
    return m.matches();
   }

class getResult implements Runnable{
	public void run() {
		while(isrun==0){
		if(isrun==2)
			break;
		isrun=2;
		List params=new ArrayList();
		params.add(new BasicNameValuePair("ASK","Find"));
		params.add(new BasicNameValuePair("email",eString));
		PostMessage postMessage=new PostMessage(params);
		 result=postMessage.getResult();
		Message msg=handler.obtainMessage();
		if(result.equals("-1"))
		msg.what=-1;
		else {
		msg.what=1;
		}
		handler.sendMessage(msg);
		isrun=1;
		}
	}
}
class EmailHandler extends Handler{
	public void handleMessage(Message msg) {
		if(msg.what==1){
		Toast.makeText(context,result, Toast.LENGTH_LONG).show();	
		}else{
		Toast.makeText(context,R.string.neterror, Toast.LENGTH_LONG).show();
		}
	dialog.dissmissDialog();
	}
}

}
